﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Collections;

/// <summary>
/// Summary description for LichDien
/// </summary>
/// 
public  class ChuongTrinh_LichDien
{
    public lichdien ld;
    public chuongtrinh ct;
}
public class LichDien : DBHelper
{
	public LichDien()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public static IEnumerable GetAll(int start, int take, ref int total)
    {
        using(var db = new LanAnhDataContext(ConnectionString))
        {

            //var lichdien = db.lichdiens.Join(db.chuongtrinhs, d => d.mact, b => b.mact, (d, b) => new { ld = d, ct = b }).Skip(start).Take(take);
            total = db.chuongtrinhs.Count();
            var chuongtrinh = db.chuongtrinhs.Select(c => new
                                                {
                                                    ct = c,
                                                    tenLoaiCt = db.loaichuongtrinhs.Single(lc => lc.maloaict == c.maloaict),
                                                    ld = db.lichdiens.Where(l => l.mact == c.mact).Select(l => (l)),
                                                    total = db.lichdiens.Where(l => l.mact == c.mact).Select(l => (l)).Count().ToString()
                                                }).OrderByDescending(c => c.ct.mact).Skip(start).Take(take);
            //var chuongtrinh = db.sp_ViewAllLichDien_Limit(take, start);
            return chuongtrinh.ToList();
        }
    }

    public IEnumerable GetData(int id)
    {
        var db = new LanAnhDataContext(ConnectionString);
        var lichdien =
            db.lichdiens.Join(db.chuongtrinhs, ld => ld.mact, ct => ct.mact, (ld, ct) => new {ld, ct}).Where(
                @t => @t.ld.mact == id).Select(@t => new {@t.ld, @t.ct.mact, @t.ct.tenct});
        return lichdien.ToArray();
    }

    public static IEnumerable GetByID(int idct,ref int count) // ID of 'Chuong Trinh'
    {
        using(var db = new LanAnhDataContext(ConnectionString))
        {
            Nullable<int> result = null;
            IEnumerable lichdien = null;
            if (check == 1)
            {
                lichdien = db.sp_XemLichDien_Error_UnrepeatableRead(idct,ref result).ToList();
            }
            else
            {
                lichdien = db.sp_XemLichDien_Fix_UnrepeatableRead(idct, ref result).ToList();
            }
            var chuongtrinh = db.chuongtrinhs.Where(c => c.mact == idct).Select(c => new
            {
                ct = c,
                tenLoaiCt = db.loaichuongtrinhs.Single(lc => lc.maloaict == c.maloaict),
                ld = lichdien,
                total = result.ToString()
            });
            count = int.Parse(result.ToString());
            return chuongtrinh.ToArray();
        }
    }

    public static bool CheckLichDien(int idld) // ID of 'Chuong Trinh'
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            var lichdien = db.lichdiens.Where(ld => ld.mald == idld);
            if (lichdien.Count() > 0)
                return true;
            return false;
        }
    }

    public static IEnumerable ViewAll()
    {
        using(var db = new LanAnhDataContext(ConnectionString))
        {
            var lichdien= db.sp_ViewAllLichDien();
            return lichdien.ToList();
        }
    }

    public void deleteLichdien(int maLd)
    {
        var db = new LanAnhDataContext(ConnectionString);
        db.sp_XoaLichDien_UnrepeatableRead(maLd);
    }

    public void updateLichdien(int songay, int mald)
    {
        var db = new LanAnhDataContext(ConnectionString);
        if (check == 1)
        {
            db.sp_CapNhatLichDien_Error_LostUpdate_Transaction1(songay, mald);
        }
        else
        {
            db.sp_CapNhatLichDien_Fix_LostUpdate_Transaction1(songay, mald);
        }
    }

    public void insertLichdien(DateTime ngaydien, TimeSpan giobatdau, TimeSpan gioketthuc, int mact )
    {
        var db = new LanAnhDataContext(ConnectionString);
        db.sp_ThemLichDien_Phantom(ngaydien, giobatdau, gioketthuc, mact);
    }

    public static IEnumerable XemLichDien_PhanTom(int mact, ref int total)
    {
        using (var db = new LanAnhDataContext(ConnectionString))
        {
            IEnumerable ctrinh = null;
            Nullable<int> result = null;
            if (check == 1)
            {
                ctrinh = db.sp_XemLichDien_Error_Phantom(mact, ref result).ToList();                
            }
            else
                ctrinh = db.sp_XemLichDien_Fix_Phantom(mact, ref result).ToList();
            total = Convert.ToInt32(result);
            return ctrinh;
        }
    }
}